package com.hws.leetcode.Day_01_19;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * ClassName:leetcode_219
 * Package:com.hws.leetcode.Day_01_19
 * Description:
 *
 * @ date:2022/1/19 11:04
 * @ author:hws
 */
public class leetcode_219 {

    public boolean containsNearbyDuplicate1(int[] nums, int k) {
        if (nums.length == 0) {
            return false;
        }
        int i = 0;
        while (i < nums.length) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] == nums[j] && (Math.abs(i - j) <= k)) {
                    return true;
                }
            }
            i++;
        }
        return false;
    }

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums.length == 0) {
            return false;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (set.size() > k) {
                set.remove(nums[i - k - 1]);
            }
            if (!set.add(nums[i])) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        leetcode_219 lc = new leetcode_219();
        int[] nums = {11, 0, 11, 11, 12, 13};
        System.out.println(lc.containsNearbyDuplicate(nums, 1));
    }

}
